public class Solution427 {

    static class Node {
        public boolean val;
        public boolean isLeaf;
        public Node topLeft;
        public Node topRight;
        public Node bottomLeft;
        public Node bottomRight;


        public Node() {
            this.val = false;
            this.isLeaf = false;
            this.topLeft = null;
            this.topRight = null;
            this.bottomLeft = null;
            this.bottomRight = null;
        }

        public Node(boolean val, boolean isLeaf) {
            this.val = val;
            this.isLeaf = isLeaf;
            this.topLeft = null;
            this.topRight = null;
            this.bottomLeft = null;
            this.bottomRight = null;
        }

        public Node(boolean val, boolean isLeaf, Node topLeft, Node topRight, Node bottomLeft, Node bottomRight) {
            this.val = val;
            this.isLeaf = isLeaf;
            this.topLeft = topLeft;
            this.topRight = topRight;
            this.bottomLeft = bottomLeft;
            this.bottomRight = bottomRight;
        }
    }

    public Node construct(int[][] grid) {
        return con(0,0,grid.length,grid);
    }

    private Node con(int x, int y, int len, int[][] grid) {
        int cur=grid[x][y];
        boolean flag=true;
        for (int i=x;i<x+len;i++){
            for (int j=y;j<y+len;j++){
                if (grid[i][j] != cur) {
                    flag = false;
                    break;
                }
            }
        }
        if (flag){
            boolean f= grid[x][y] != 0;
            return new Node(f,true);
        }else{
            return new Node(false,false,con(x,y,len/2,grid),con(x,y+len/2,len/2,grid),con(x+len/2,y,len/2,grid),con(x+len/2,y+len/2,len/2,grid));
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution427().construct(new int[][]{{0,1},{1,0}}).bottomLeft.val);
    }
}
